<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="680px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '添加设备' : '添加设备'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
            <el-form-item label="商户:" prop="roleIds">
              <role-select v-model="form.roleIds" />
            </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <prize ref="prize"  @uploadPrice="uploadPrice"></prize>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import prize from './prize.vue'
  import { emailReg, phoneReg } from 'ele-admin';
  import RoleSelect from './role-select.vue';
  import { addUser, updateUser, checkExistence } from '@/api/equipmentManagement';
  import {JSEncrypt} from "jsencrypt";

  export default {
    components: { RoleSelect },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        id:'',
        equipmentName: '',
        equipmentCode: '',
        equipmentStatus: '',
        equipmentPicture: '',
        activityName:'',
        startTime:'',
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          equipmentName: [
            {
              required: true,
              message: '请输入设备名称',
              trigger: 'blur'
            }
          ],
          equipmentCode: [
            {
              required: true,
              message: '请输入设备编号',
              trigger: 'blur'
            }
          ],
          equipmentPicture: [
            {
              required: true,
              message: '请选择设备图片',
              trigger: 'blur'
            }
          ],
          activityName: [
            {
              required: true,
              message: '请输入活动名称',
              trigger: 'blur'
            }
          ],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      uploadPrice(e){
        console.log('e',e)
        // return
        this.form.agentMealList=e
        this.confirm()
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form,
          };
          const saveOrUpdate = this.isUpdate ? updateUser : addUser;
          if (!this.isUpdate) {
            let encryptor = new JSEncrypt()
            let pk = this.$store.state.signature.pk
            encryptor.setPublicKey(pk)
            data.password = encryptor.encrypt(data.password)
          }
          saveOrUpdate(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data,
              password: null,
              sex: this.data.sex + ''
            });
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
